﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHibernateDB;
using FTPLib;
using SmartAnnotation;

namespace MedicalDB
{
    public partial class ImaginiMedicaleForm : UserControl
    {
        string CNP;
        string limba;

        int _previouslySortedColumn = -1;

        public ImaginiMedicaleForm(string CNP,string limba)
        {
            InitializeComponent();
            Rename(limba);
            this.limba = limba;
            this.CNP = CNP;

            lvImaginiMedicale.View = View.Details;
            lvImaginiMedicale.FullRowSelect = true;
            lvImaginiMedicale.GridLines = false;
          

            string xmlFile = @"C:\Language" + limba + ".xml";
            XMLParser xmlParser = new XMLParser(xmlFile);
            List<string> namesForm = xmlParser.ExtractAttribute(lvImaginiMedicale.Name.ToString(), string.Empty, string.Empty, "name");

            foreach (string s in namesForm)
                lvImaginiMedicale.Columns.Add(s);

            PersistenceManager pm = PersistenceManager.Instance;
            IList<ImaginiMedicale> listimagini = pm.RetrieveEquals<ImaginiMedicale>("IdPacient", CNP);
            populateListView(listimagini);
        }

        private void ListaPacienti_DoubleClick(object sender, EventArgs e)
        {
            for (int i = 0; i < lvImaginiMedicale.Items.Count; i++)
            {
                if (lvImaginiMedicale.Items[i].Selected == true)
                {

                    ListViewItem itm = lvImaginiMedicale.SelectedItems[0];

                    // download image
                    DescarcaImaginiMedicaleForm dim = new DescarcaImaginiMedicaleForm(itm.SubItems[4].Text);
                    dim.ShowDialog();

                    // view image
                    if (dim.success == true)
                    {
                        SmartAnnotationForm smartAnotation = new SmartAnnotationForm(itm.SubItems[0].Text, itm.SubItems[4].Text, limba);
                        smartAnotation.Show();
                    }

                    dim.Dispose();
                    dim = null;
                 
                    // delete image
                    //System.IO.File.Delete(itm.SubItems[4].Text);
                }

            }
        }
        
        private void populateListView(IList<ImaginiMedicale> list)
        {
            foreach (ImaginiMedicale imed in list)
            {
                ListViewItem item = new ListViewItem(imed.Id.ToString());
                item.SubItems.Add(imed.Tip);
                item.SubItems.Add(imed.DescriereImagine);
                item.SubItems.Add(imed.Data.ToString());
                item.SubItems.Add(imed.Locatie);

                lvImaginiMedicale.Items.Add(item);
            }

            foreach (ColumnHeader ch in this.lvImaginiMedicale.Columns)
            {
                ch.Width = -2;
            }
        }

        void listview1_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {

            ListViewComparer lvComparer;
            if (null == lvImaginiMedicale.ListViewItemSorter)
            {
                lvComparer = new ListViewComparer();
            }
            else
            {
                lvComparer = (ListViewComparer)lvImaginiMedicale.ListViewItemSorter;
            }
            if (e.Column != _previouslySortedColumn)
            {
                lvImaginiMedicale.Sorting = SortOrder.None;
            }
            if (lvImaginiMedicale.Sorting == SortOrder.Ascending)
            {
                lvImaginiMedicale.Sorting = SortOrder.Descending;
            }
            else
            {
                lvImaginiMedicale.Sorting = SortOrder.Ascending;
            }
            LVSort.SetSortIcons(lvImaginiMedicale, ref _previouslySortedColumn, e.Column);
            lvComparer.SortColumn = e.Column;
            lvImaginiMedicale.ListViewItemSorter = (System.Collections.IComparer)lvComparer; //this immediately causes a sort to happen

            foreach (ColumnHeader ch in this.lvImaginiMedicale.Columns)
            {
                ch.Width = -2;
            }
        }
    }
}
